//
//  CloudReview.m
//  GuessTheBeautyFinger
//
//  Created by How Charles on 11-11-19.
//  Copyright 2011 hust. All rights reserved.
//

#import "CloudReview.h"  

/*
 单例模式
 */

@implementation CloudReview  
static CloudReview* _sharedReview = nil;  
+(CloudReview*)sharedReview  
{  
	@synchronized([CloudReview class])  
	{  
		if (!_sharedReview)  
		{
			[[self alloc] init];  
		}
		
		return _sharedReview;  
	}  
	
	return nil;  
}  
+(id)alloc  
{  
	@synchronized([CloudReview class])  
	{  
		NSAssert(_sharedReview == nil, @"Attempted to allocate a second instance of a singleton.");  
		_sharedReview = [super alloc];  
		return _sharedReview;  
	}  
	
	return nil;  
}  
-(void)reviewFor:(int)appleID  
{  
	m_appleID = appleID;  
	BOOL neverRate = [[NSUserDefaults standardUserDefaults] boolForKey:@"neverRate"];  
	if(neverRate != YES) {  
		//Show alert here  
		UIAlertView *alert;  
		alert = [[UIAlertView alloc] 
				 initWithTitle:NSLocalizedString(@"rate_app_title",@"Rate My Appication")  
				 message:NSLocalizedString(@"rate_app_message",@"Please Rate my Application")  
										  delegate: self  
								 cancelButtonTitle:NSLocalizedString(@"rate_cancel",@"Cancel")  
								 otherButtonTitles: NSLocalizedString(@"rate_now",@"Rate Now"),  
				 NSLocalizedString(@"rate_never",@"Never Rate"), nil];  
		[alert show];  
		[alert release];  
	}  
}  

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex  
{  
	// Never Review Button  
	if (buttonIndex == 2)  
	{  
		[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"neverRate"];  
	}  
	// Review Button  
	else if (buttonIndex == 1)  
	{  
		[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"neverRate"];  
		NSString *str = [NSString stringWithFormat:  
						 @"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=%d",  
						 m_appleID ];   
		myNSLog(str);  
		[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];  
	}  
}  


/*
 调用过程
 [[CloudReview sharedReview] reviewFor:395519376];
 [[CloudReview sharedReview] reviewThisApp];
 */

-(void)reviewThisApp
{
	[[CloudReview sharedReview] reviewFor:THIS_APP_ID_IN_APP_STORE];
}

@end 


